import java.io.*;

public class TT {
        public static void main(String[] args) {
            Employee[] employees = new Employee[]{
                    new Employee("John", 2000),
                    new Employee("Mary", 2500),
                    new Employee("Bob", 2200)
            };

            try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("employees.dat"));
                 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("employees.dat"))) {

                // 写入对象到文件
                for (Employee employee : employees) {
                    oos.writeObject(employee);
                }

                // 读取对象并打印
                for (int i = 0; i < employees.length; i++) {
                    Employee employee = (Employee) ois.readObject();
                    System.out.println(employee);
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    class Employee implements Serializable {
        private String name;
        private double salary;

        public Employee(String name, double salary) {
            this.name = name;
            this.salary = salary;
        }

        public String toString() {
            return "Employee: " + name + ", salary: " + salary;
        }
    }

